import { API_BASE_URL } from '@/config'
import { getToken } from '@/utils/auth'
import { msg } from '@/utils/helper'

const request = (config) => {
	

	config.url = API_BASE_URL + config.url
	if(!config.data) {
		config.data = {}
	}

	config.header = {
		'Token': getToken()
	}

	console.log('请求参数', JSON.stringify(config.data))

	let promise = new Promise((resolve, reject) => {
	    uni.request(config).then(response => {

			// 异常
			if(response[0]){

				reject({message : '网络超时'})
			}else{

				let res = response[1].data
				// 处理拦截器
				if(res.code != 200){
					console.log('请求出错：', res)
					msg(res.msg)
					reject(res)
				}

				resolve(res)
			}
		}).catch(error => {
			console.log(error)
			reject(error)
		})
	})
	
	return promise
}

export default request